package com.example.demo.controller;

import com.example.demo.pojo.*;
import com.example.demo.service.Impl.LifebookServiceImpl;
import com.example.demo.service.Impl.LogTestServiceImpl;
//import com.example.demo.utils.JwtUtils;
import com.example.demo.service.Impl.RemittanceServiceImpl;
import com.example.demo.service.Impl.SettingServiceImpl;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;
import net.sf.jsqlparser.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.example.demo.utils.TokenUtils.token;


@RestController
@RequestMapping("/api")
public class LogTestController {
    @Autowired
    private LogTestServiceImpl logTestService;
    @Autowired
    private LifebookServiceImpl lifebookService;
    @Autowired
    private RemittanceServiceImpl remittanceService;
    @Autowired
    private SettingServiceImpl settingService;
//    LogTest logTest = new LogTest();
    @PostMapping("/login")
    public Result querypasswd(@RequestBody LogTest logTest){

        System.out.println("员工登录：{}"+logTest);
//        调用service层，查询员工用户名及密码
        LogTest logTests = logTestService.selectLog(logTest);
        if (logTests != null){
            if (logTests.getStatus() != 1){
                String token = token(logTests.getUsername(),logTests.getPassword(),logTests.getRole());
                System.out.println(token);
                lifebookService.xiugai();
//            返回给前端
                return ResultLog.success(token);
            }
            else return Result.error("该用户已被禁用");
//            登录成功，生成令牌，下发令牌
//            Map<String ,Object> claims = new HashMap<>();
//            claims.put("username",logTest.getUsername());
//            claims.put("password",logTest.getPassword());
//            String s = JwtUtils.generateJwt(claims);//jwt包含了当前登录的员工信息

        }else {
//            登录失败，返回错误信息
            return Result.error("登录失败,用户名或密码错误");
        }

    }

    @PostMapping("/lifebook/export")
    public void  lifebookExport(@RequestBody Abc abc, HttpServletResponse resp){
        byte[] bytes = lifebookService.lifebookExport(abc.getInner());
        try {
            resp.getOutputStream().write(bytes);
            resp.flushBuffer();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/user/addmerit")
    // 敲木鱼加功德
    public Result useraddmerit(@RequestBody Map<String,String> uniqueid){
        int value = settingService.selectValue("woodenfish");
        if (value == 1){
            String string = uniqueid.get("uniqueid");
            lifebookService.addmerit(string);
            return Result.success("成功");
        }else {
            return Result.error("无法添加功德，该功能已关闭");
        }
    }
    @PostMapping("/remittance")
    // 添加汇款
    public Result remittanceAdd(@RequestBody Remittance remittance){
        int value = settingService.selectValue("remittance");
        if (value == 1){
            remittanceService.remittanceAdd(remittance);
            remittanceService.remittanceUpdatelifebook(remittance);
            Remittance yinmoney = remittanceService.remittanceSelectlifebook(remittance);
            return PageResult.success02(yinmoney);
        }else {
            return Result.error("无法汇款，该功能已关闭");
        }

    }
    //普通用户获取配置项
    @GetMapping("/systemset")
    public Result settingPuTongSelect(String key){
        List<Setting> settings= settingService.settingPuTongSelect(key);
        return PageResult.success02(settings);
    }
    //普通用户获取临时授权
    @GetMapping("/user/gettempauth")
    public Result huoqulinshi(){
        int role = 5;
        LogTest logTest = logTestService.huoqulinshi(role);
        String token = token(logTest.getUsername(),logTest.getPassword(),logTest.getRole());
        return ResultLog.success(token);
    }

}


@Data
class Abc{
    private String inner;
}